package cn.xerllent.wxtp.handler;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpTpXmlPackage;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;

import java.util.Map;

/**
 * @author 
 */
@Component
public class MsgHandler extends AbstractHandler {
	
	
	  @Override
	  public WxCpXmlOutMessage handle(WxCpTpXmlMessage wxMessage,
	              Map<String, Object> context,
	              WxCpTpService wxCpTpService,
	              WxSessionManager sessionManager) {
	        final String msgType = wxMessage.getMsgType();
	        if (msgType == null) {
	            // 如果msgType没有，就自己根据具体报文内容做处理
	        }

	        if (!msgType.equals(WxConsts.XmlMsgType.EVENT)) {
	            //TODO 可以选择将消息保存到本地
	        }

	        //TODO 组装回复消息
	        String content = "收到信息内容：" + JSONUtil.toJsonStr(wxMessage);
	        
	        WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content(content)
            .fromUser(wxMessage.getToUserName())
            .toUser(wxMessage.getAllFieldsMap().get("FromUserName").toString())
            .build();
		    return m;
	  }
	  
}
